<?php
class ConsumelogAction extends CommonAction {


	//会员当天的产品使用记录
	public function todayConsumelog(){
		$memberId     = $_REQUEST['memberId'];
		$year         = date('Y',time());
		$month        = date('m',time());
		$day          = date('d',time());
		$today        = strtotime("$year-$month-$day");
		$Consumelog   = M('Consumelog');
		$consumelog      = $Consumelog->where('memberId='.$memberId.' AND addTime>'.$today)->order('addTime Desc')->select();

		$Role         = M('Role');  // 获取技师
		$role_sale    = $Role ->getByName('技师');
		$role_id      = $role_sale[id];
		$Teach        = M('User');
		$teach        = $Teach->join('dudu_role_user on id= dudu_role_user.user_id')
				              ->where("dudu_role_user.role_id=$role_id")
				              ->select();
	    $this->assign('teach', $teach);

		$Productlog  = M('Productlog'); //获取可使用的已购买产品
		$productlog  = $Productlog->where("memberId=$memberId AND useTimes>usedTimes")->order('addTime DESC')->select();
		$this->assign('productlog', $productlog);

		$role_sale        = $Role ->getByName('销售顾问');// 获取销售顾问
		$role_id          = $role_sale[id];
		$Saler            = M('User');
		$salers           = $Saler
				          ->join('dudu_role_user on id= dudu_role_user.user_id')
				          ->where("dudu_role_user.role_id=$role_id")
				          ->select();
		$this->assign("salers",$salers);

		$Product = M('Product');  //产品列表
		$products = $Product->where('status=1')->order('typeId desc')->select();
		$this->assign('products', $products);

		$this->assign('consumelog',$consumelog);
		$Card = M('Card');
		$card = $Card->where('memberId='.$memberId)->find();
		$this->assign('card',$card);
		$this->display();
	}

	//添加产品使用记录
	public function add(){
		$data = array();
		$data['productId'] = $_REQUEST['productId'];
		$data['teachId'] = $_REQUEST['teachId'];
		$data['room'] = $_REQUEST['room'];
		$data['memberId'] = $_REQUEST['memberId'];
		$data['consumeTime'] = $_REQUEST['consumeTime'];
		$data['addTime'] = time();
		$Consumelog = M('Consumelog');
		$Consumelog->startTrans();
		$consumeLog = $Consumelog->add($data);
		if($consumeLog){//更新所购买的产品信息
			$Productlog = M('Productlog');
			$r = $Productlog->setInc('usedTimes',"id={$_REQUEST['plId']}",1);
			if($r){
				$Consumelog->commit(); //事务提交
				$this->assign ( 'jumpUrl', "__URL__/todayConsumelog/memberId/{$_REQUEST['memberId']}/fff" );
				$this->success ('添加成功!');
				return;
			}
		}
		$Consumelog->rollback(); //事务回滚
		$this->error ('添加失败!');
	}

}
?>